home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
MNetsrc.hqx
/
Mac TCP_IP Source v.33
/
mac_files.c
< prev
next >
Wrap
Text File
|
1989-03-27
|
14KB
|
585 lines
/* mac_files
* This module contains all the directory stuff within the KA9Q package
*/
#include <stdio.h>
#include "global.h"
#include "mac.h"
#include "cmdparse.h"
#include "mac_files.h"
#include <HFS.h>
extern errno;
extern struct RemoveIt Head;
char *CtoPstr(),*PtoCstr(); /* used to interface to Toolbox */
/*
* dir: Create a directory listing in a temp file and return the resulting file
* descriptor. If full == 1, give a full listing in the temp file; if full
* == 2, give just a list of names in the temp file; if full ==3, output to
* console with the full listing. full == 0 is same as full == 2.
*/
FILE *
dir(path,full)
char *path;
int full;
{
WDPBRec MyDisk;
CInfoPBRec Everything;
FILE *fp, *fp1;
OSErr e;
char *GetPathname();
char *ptr;
char buff[256];
char holding_file[256];
char working_volume[256];
char dirname[256];
errno = 0;
/* open up dirnet.temp file for the cases we need it, otherwise direct to stdout */
if ( full < 3) {
if ( ( fp = fopen(dirnet, "w")) == NULL) {
printf("Open failed, errno = %d\n",errno);
return((FILE *)NULL);
}
}
else
fp = stdout;
/* start process of getting directory */
MyDisk.ioWDProcID = 0L;
MyDisk.ioWDDirID = 0L;
MyDisk.ioVRefNum = 0;
MyDisk.ioWDVRefNum = 0;
MyDisk.ioNamePtr = (StringPtr)working_volume;
if (path[0] == NULL)
path = ":";
MoveIt(MyDisk.ioNamePtr, path);
if ( (e = PBOpenWD( &MyDisk, FALSE)) != noErr ) {
/*
* got an error on initial PBOpenWD
* see if we were passed a file name instead of directory by trying to
* open it through the library fopen call
*/
if ( ( fp1 = fopen(path, "r")) == NULL) {
if (full < 3) {
fclose(fp);
unlink(dirnet);
}
else
printf("Can't get directory of '%s'.\n",path);
return(NULL);
}
else {
fclose(fp1); /* close the fopen file */
/* we were able to open the path as a file.
* For just the list of file names, strip the latter part
* of the path and place that in the file. */
if ( (full == 0) || (full == 2) ) {
ptr = rindex(path, ':');
if (ptr != NULL) /* found a :, return stuff to right */
ptr++;
else
ptr = path; /* no :, return entire path */
fprintf(fp,"%s\n", ptr);
fclose(fp); /* close file, reopen as read only */
fp = fopen(dirnet, "r");
return(fp);
}
else {
/* we have a single file, and we must return full info. Get it. */
sprintf(holding_file, "%s", path);
CtoPstr(holding_file);
Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
Everything.hFileInfo.ioVRefNum = MyDisk.ioVRefNum;
Everything.hFileInfo.ioFDirIndex = 0;
Everything.hFileInfo.ioDirID = 0L;
Everything.hFileInfo.ioCompletion = 0;
if ( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr) {
ptr = ctime((long *)&Everything.hFileInfo.ioFlCrDat);
ptr +=3;
ptr[strlen(ptr)-1] = '\0';
ptr[strlen(ptr)-8] = '\0';
fprintf(fp,"%c %8ld %8ld-rf %s %s %s\n",
(Everything.hFileInfo.ioFlAttrib & (1<<4))?'d':'-',
(Everything.hFileInfo.ioFlAttrib & (1<<4))?
0:Everything.hFileInfo.ioFlLgLen,
(Everything.hFileInfo.ioFlAttrib & (1<<4))?
0:Everything.hFileInfo.ioFlRLgLen, ptr, ptr+19,
PtoCstr((char *) Everything.hFileInfo.ioNamePtr));
}
else
printf("Error getting directory: %d\n", e);
}
if ( full < 3 ) {
fclose(fp);
fp = fopen(dirnet, "r");
}
return(fp);
}
}
/* original PBOpenWD worked */
if (full == 3)
fprintf(fp,"Directory of %s\n",GetPathname(dirname,MyDisk.ioVRefNum));
Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
Everything.hFileInfo.ioVRefNum = MyDisk.ioVRefNum;
sprintf(Everything.hFileInfo.ioNamePtr, "\p");
Everything.hFileInfo.ioFDirIndex = 1;
Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
Everything.hFileInfo.ioCompletion = 0;
while( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr) {
if ( e == noErr ) {
if( full == 0) {
fprintf(fp, "%s\n",
PtoCstr((char *) Everything.hFileInfo.ioNamePtr));
}
else {
ptr = ctime((long *)&Everything.hFileInfo.ioFlCrDat);
ptr +=3;
ptr[strlen(ptr)-1] = '\0';
ptr[strlen(ptr)-8] = '\0';
fprintf(fp,"%c %8ld %8ld-rf %s %s %s\n",
(Everything.hFileInfo.ioFlAttrib & (1<<4))?'d':'-',
(Everything.hFileInfo.ioFlAttrib & (1<<4))?
0:Everything.hFileInfo.ioFlLgLen,
(Everything.hFileInfo.ioFlAttrib & (1<<4))?
0:Everything.hFileInfo.ioFlRLgLen, ptr, ptr+19,
PtoCstr((char *) Everything.hFileInfo.ioNamePtr));
}
Everything.hFileInfo.ioFlLgLen = 0;
Everything.hFileInfo.ioFDirIndex++;
}
else
break; /* go an error other than fnfErr: break */
Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
}
/*
* either the PBGetCatInfo finally encountered a fnfErr, or some other error
* occurred. If we were writing a file, close it then reopen it before returning
* the file pointer to our caller.
*/
PBCloseWD(&MyDisk, FALSE);
if ( full < 3) {
fclose(fp);
fp = fopen(dirnet, "r");
}
return(fp);
}
/*
* mktemp: make temporary filename and return a pointer to it.
*
* method: if the string passed has an X in it, insert an 8-digit hexadecimal
* number based on the current tick count starting at the first X position. If the
* string passed has no X, insert the 8-digit hexadecimal number starting at
* the beginning of the string. No action is taken if there is insufficient room
* to insert the number without extending the passed string.
*/
char *mktemp(ff)
char *ff;
{
char *ptr;
if ( ( ptr = index(ff, 'x') ) == NULL)
ptr = ff;
if (strlen(ptr) >= 8)
sprintf(ptr, "%lx", (long)TickCount());
return(ff);
}
static struct RemoveIt search_result;
/*
* filedir: search for a file.
*
* NOTE: this is not a general file search routine.
* The search path must have the filename in the form xxxxx.yyyyy. This search
* code will look for files that match the .yyyyy.
*
* filedir(path,value,dest)
* The name of the matched file (just the filename, no folder/path info) will
* be returned in dest. An empty dest (first value NULL) indicates no match.
*
* value =0: start a new search thread.
* value =1: use the last search thread, provide the next file match.
*/
int filedir(path,value,dest)
char *path;
int value;
char *dest;
{
WDPBRec MyDisk;
CInfoPBRec Everything;
FILE *fp1;
OSErr e;
char *GetPathname();
char *ptr,*name_ptr;
char keep;
char holding_file[256];
char working_volume[256];
char search_string[256];
char *NLreturn_first();
dest[0] = NULL; /* initialize return value */
/* first, see if this is a new search thread. If so, we must first clean up any
* outstanding search_result structure.
*/
if (value == 0) {
NLdelete_all(&search_result);
errno = 0;
/* start process of getting directory */
MyDisk.ioWDProcID = 0L;
MyDisk.ioWDDirID = 0L;
MyDisk.ioVRefNum = 0;
MyDisk.ioWDVRefNum = 0;
MyDisk.ioNamePtr = (StringPtr)working_volume;
/* establish our path name */
if (path[0] == NULL)
strcpy(search_string,":");
else
strcpy(search_string,path);
/* see if we have a path with folder info or just a filename */
if ( (ptr = rindex(search_string, ':')) != NULL) {
keep = *ptr;
*ptr = '\0';
MoveIt(MyDisk.ioNamePtr, search_string);
*ptr = keep;
}
else
MoveIt(MyDisk.ioNamePtr, search_string);
if ( ( name_ptr = rindex( search_string, '.')) != NULL)
name_ptr++;
else
name_ptr = "";
/*open the working directory */
if ( (e = PBOpenWD( &MyDisk, FALSE) != noErr) ) {
/* got an error on initial PBOpenWD - see if we were passed a file name
* instead of directory by trying to open it through the library fopen call
*/
if ( ( fp1 = fopen(search_string, "r")) == NULL)
return(-1);
fclose(fp1); /* close the fopen file */
/* we were able to open the path as a file - return the single file name */
ptr = rindex(search_string, ':');
if (ptr != NULL) /* found a :, return stuff to right */
ptr++;
else
ptr = search_string; /* no :, return entire path */
strcpy(dest,ptr); /* copy our single result */
return(0);
}
/* original PBOpenWD worked */
Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
Everything.hFileInfo.ioVRefNum = MyDisk.ioVRefNum;
Everything.hFileInfo.ioFDirIndex = 1;
Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
Everything.hFileInfo.ioCompletion = 0;
while( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr) {
if ( e == noErr ) {
PtoCstr(holding_file);
if ( ( ptr = rindex( holding_file, '.')) != NULL) {
ptr++;
if ( strncmp(ptr, name_ptr, strlen(name_ptr)) == 0)
NLadd_name(holding_file, &search_result);
}
Everything.hFileInfo.ioFlLgLen = 0;
Everything.hFileInfo.ioFDirIndex++;
Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
}
else
break; /* got an error other than fnfErr: break */
}
/* either the PBGetCatInfo finally saw a fnfErr, or some other error occurred. */
PBCloseWD(&MyDisk, FALSE);
}
/* either we are on a search continuation or have finished the data gathering phase */
if ((ptr = NLreturn_first(&search_result)) != NULL) {
strcpy(dest,ptr);
NLdelete_first(&search_result);
return(0);
}
return(-1);
}
/*
* tmpfile: create a temporary file. Remember it so we can delete it later because
* the mac does not allow the file to be deleted when it is open.
*/
FILE *
tmpfile()
{
FILE *tmp;
char *mktemp();
char *ptr;
char tmpname[16];
strcpy(tmpname,"SMTPxxxxxxxx");
if ( ( ptr = malloc(strlen(temppath)+strlen(tmpname)+1)) == NULLCHAR) {
printf("tmpfile: can't allocate memory.\n");
return(NULL);
}
(void) mktemp(tmpname); /* replace the xxxxxxxx with something else */
sprintf(ptr,"%s%s",temppath,tmpname);
if ( ( tmp = fopen(ptr, "w+") ) == NULL) {
printf("tmpfile: could not create temp file (%s)\n", ptr);
(void)free(ptr); /* free up our original name storage */
return(NULL);
}
/* save away the name of the file so we can delete it when exiting (ignore
* errors, if we can't delete at exit it won't harm anything). */
NLadd_name(ptr,&Head);
(void)free(ptr); /* free up our original name storage */
return (tmp); /* return file pointer to the file we opened */
}
/*
* Access: check the file for access permission. Some of this has to be faked on
* a Mac, since it does not have access bits.
*
* returned value = 0: OK
* returned value = 1: reject
*/
int
access(str, perm)
char *str;
int perm;
{
FILE *fptr;
CInfoPBRec paramBlock;
OSErr e;
paramBlock.hFileInfo.ioCompletion = 0;
paramBlock.hFileInfo.ioVRefNum = 0;
paramBlock.hFileInfo.ioFDirIndex = 0;
paramBlock.hFileInfo.ioDirID = 0;
paramBlock.hFileInfo.ioNamePtr = (StringPtr) CtoPstr(str);
/*
* Get info on file named in ioNamePtr
*/
e = PBGetCatInfo( ¶mBlock, FALSE);
PtoCstr(str);
/*
* if there is an error then find out if the file is present. If so
* see if user wants to create the file
*/
if ( e != noErr ) {
if ( ( e == fnfErr) && (perm == 2))
return(0);
else
return(1);
}
/*
* check to see if the file is locked or open. refuse it if it is.
*/
if ( (BitTst( ¶mBlock.hFileInfo.ioFlAttrib, 0)
|| BitTst( ¶mBlock.hFileInfo.ioFlAttrib, 7) ) && perm == 4 )
return(1);
else
return(0);
}
/* List directory to console */
dodir(argc,argv)
int argc;
char *argv[];
{
switch (argc) {
case 1:
dir(":",3);
break;
case 2:
dir( argv[1], 3);
break;
default:
printf("Cannot accept %d arguments to DIR\n", argc-1);
return(1);
}
return (0);
}
/*
* docd: perform a change directory
*/
docd(argc, argv)
int argc;
char *argv[];
{
char dirname[256];
char *GetPathname();
int WDVRefNum;
if (argc > 1) {
if (chdir(argv[1],&WDVRefNum) != 0) {
printf("Can't change directory.\n");
return (1);
}
}
else
if (chdir(":",&WDVRefNum) != 0) {
printf("Can't report directory.\n");
return (1);
}
printf(" Path = %s\n",GetPathname(dirname,WDVRefNum));
return (0);
}
/* chdir
* This routine changes the working directory to the path specified.
* The WDVRefNum for the new directory is returned.
*/
int chdir( path, WDVRefNum )
char *path;
int *WDVRefNum;
{
WDPBRec MyDisk;
OSErr e;
char ourpath[256];
/* prep the parameter block */
MyDisk.ioWDProcID = 0L;
MyDisk.ioWDDirID = 0L;
MyDisk.ioVRefNum = 0;
MyDisk.ioWDVRefNum = 0;
/* handle our single argument */
if (path[0] == NULL)
MoveIt(ourpath,":");
else
MoveIt(ourpath,path);
MyDisk.ioNamePtr = (StringPtr)ourpath;
/* open working directory */
if ((e = PBOpenWD( &MyDisk, FALSE)) != noErr)
return(-1);
/* make the working directory the default */
MyDisk.ioNamePtr = NULL;
if ( ( e = PBHSetVol( &MyDisk, FALSE) ) != noErr)
return(-1);
/* return the VRefNum in case the caller wants to generate a pathname */
*WDVRefNum = MyDisk.ioVRefNum;
return(0);
}
mkdir(name, mode)
char *name;
int mode;
{
HParamBlockRec paramBlock;
int e;
paramBlock.fileParam.ioCompletion = 0;
paramBlock.fileParam.ioNamePtr = (StringPtr) CtoPstr(name);
paramBlock.fileParam.ioDirID = 0L;
paramBlock.fileParam.ioVRefNum = 0;
if ( ( e = PBDirCreate(¶mBlock, FALSE)) != 0 ) {
printf("Could not create directory, error = %d\n", e);
return(-1);
}
return(0);
}
rmdir(name)
char *name;
{
HParamBlockRec paramBlock;
int e;
paramBlock.fileParam.ioCompletion = 0;
paramBlock.fileParam.ioNamePtr = (StringPtr) CtoPstr(name);
paramBlock.fileParam.ioFVersNum = 0;
paramBlock.fileParam.ioVRefNum = 0;
if ( ( e = PBDelete(¶mBlock, FALSE)) != 0 ) {
printf("Could not delete directory, error = %d\n", e);
return(-1);
}
return(0);
}
GetFileInfo(vol,name,iop)
short vol;
char *name;
FileParam *iop;
{
iop->ioNamePtr = (StringPtr)name;
iop->ioVRefNum=vol;
iop->ioFVersNum=iop->ioFDirIndex=0;
PBGetFInfo(iop, FALSE);
}
SetFileInfo(vol,name,iop)
short vol;
char *name;
FileParam *iop;
{
iop->ioNamePtr = (StringPtr)name;
iop->ioVRefNum=vol;
iop->ioFVersNum=iop->ioFDirIndex=0;
PBSetFInfo(iop, FALSE);
}
MakeTextFile(vol,name,iop)
short vol;
char *name;
FileParam *iop;
{ GetFileInfo(vol,name,iop);
iop->ioFlFndrInfo.fdType='TEXT';
iop->ioFlFndrInfo.fdCreator='EDIT';
SetFileInfo(vol,name,iop);
}